package com.kuang.demo2;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Date;

/**
 * @Title:
 * @Description: 客户端
 * <p>
 * 原型模式：
 * spring 中的bean 的创建有两种 ：一种是单例模式 ，二是原型模式
 * @Author: zx
 * @Date: create in 10:10 2020/8/6
 * @Version: 1.0
 */
public class Bilibili {
    public static void main(String[] args) throws CloneNotSupportedException {
        clone1();
        System.out.println("\n");
        clone2();
    }

    /**
     * 克隆对象
     *
     * @throws CloneNotSupportedException
     */
    public static void clone1() throws CloneNotSupportedException {
        Date date = new Date();
        //原型对象 video1
        Video video = new Video("UP主A-视频一", DateUtil.date());
        System.out.println("video1 -> " + video);
        System.out.println("video1 hash-> " + video.hashCode());

        // UP主B 克隆 UP主A的视频
        Video video2 = (Video) video.clone();
        date.setTime(1596698749);
        video2.setName("UP主B-视频一");
        System.out.println("video2 -> " + video2);
        System.out.println("video2 hash-> " + video2.hashCode());

        System.out.println("UP主A-视频一 == UP主B-视频一:" + (video == video2));
    }

    /**
     * 验证是否是浅克隆
     *
     * @throws CloneNotSupportedException
     */
    public static void clone2() throws CloneNotSupportedException {
        //克隆对象video3
        Date date = new Date();
        Video video = new Video("UP主A-视频一", date);
        Video video2 = (Video) video.clone();

        System.out.println("video1->" + video);
        System.out.println("video1 hash-> " + video.hashCode());
        System.out.println("video2->" + video2);
        System.out.println("video2 hash-> " + video2.hashCode());

        System.out.println("=================分割线==================");
        /*
            修改了date的值，video和video2 中的date都发生了变化
            说明video1和video2同时指向了date,video2只是把video的值和引用copy过来了，是浅克隆
         */
        date.setTime(1596698749);
        System.out.println("video1->" + video);
        System.out.println("video1 hash-> " + video.hashCode());
        System.out.println("video2->" + video2);
        System.out.println("video2 hash-> " + video2.hashCode());
    }

    public String formatDate(Date date){
        return DateUtil.format(date, "yyyy-MM-dd hh:mm:ss");
    }
}
